package phase2.rd;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * A list of Tokens backed by java.util.LinkedList. If the list is empty, a
 * "null object" is returned that is not equal to anything (instead of throwing
 * an IndexOutOfBoundsException).
 * 
 * @author John Girata
 * @version Dec 8, 2012
 */
public class TokenList {
	private List<Token> list;

	public TokenList() {
		list = new LinkedList<>();
	}

	public TokenList(TokenList list) {
		this();
		addAll(list);
	}

	public int size() {
		return list.size();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public boolean contains(Object o) {
		return list.contains(o);
	}

	public Iterator<Token> iterator() {
		return list.iterator();
	}

	public Object[] toArray() {
		return list.toArray();
	}

	public <T> T[] toArray(T[] a) {
		return list.toArray(a);
	}

	public boolean add(Token e) {
		return list.add(e);
	}

	public boolean remove(Object o) {
		return list.remove(o);
	}

	public boolean containsAll(Collection<?> c) {
		return list.containsAll(c);
	}

	public boolean addAll(Collection<? extends Token> c) {
		return list.addAll(c);
	}
	
	public boolean addAll(TokenList other) {
		return addAll(other.list);
	}

	public boolean addAll(int index, Collection<? extends Token> c) {
		return list.addAll(index, c);
	}
	
	public boolean addAll(int index, TokenList other) {
		return addAll(index, other.list);
	}

	public boolean removeAll(Collection<?> c) {
		return list.removeAll(c);
	}

	public boolean retainAll(Collection<?> c) {
		return list.retainAll(c);
	}

	public void clear() {
		list.clear();
	}

	@Override
	public boolean equals(Object o) {
		return list.equals(o);
	}

	@Override
	public int hashCode() {
		return list.hashCode();
	}

	public Token get(int index) {
		if (isEmpty()) {
			return new NullToken();
		}
		
		return list.get(index);
	}

	public Token set(int index, Token element) {
		return list.set(index, element);
	}

	public void add(int index, Token element) {
		list.add(index, element);
	}

	public Token remove(int index) {
		if (isEmpty()) {
			return new NullToken();
		}
		
		return list.remove(index);
	}

	public int indexOf(Object o) {
		return list.indexOf(o);
	}

	public int lastIndexOf(Object o) {
		return list.lastIndexOf(o);
	}

	public ListIterator<Token> listIterator() {
		return list.listIterator();
	}

	public ListIterator<Token> listIterator(int index) {
		return list.listIterator(index);
	}

	public List<Token> subList(int fromIndex, int toIndex) {
		return list.subList(fromIndex, toIndex);
	}
	
	@Override
	public String toString() {
		return list.toString();
	}

	public static class NullToken extends Token {
		public NullToken() {
			this(null, null);
		}

		public NullToken(Type type, String value) {
			super(type, value);
		}

		@Override
		public boolean equals(Object other) {
			return other instanceof NullToken;
		}

		@Override
		public int hashCode() {
			return 11;
		}

		@Override
		public String toString() {
			return "NullToken";
		}
	}
}